﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;

namespace Qplus.UI
{
    public partial class PersonalSetting : PhoneApplicationPage
    {
        //public string name;
        //public int sex;
        //public string mail;
        //public string birthday;
        //public int canStrangerChat;
        //public string level;
        //public int headType;
        //public string headValue;
        //public MightList[] MightList;
        //public string Current_max_friend_count;
        //public string Max_friend_count_upper;
        //public string Task_score;
        //public appList[] appList;
        userInfo userinfo;
        public PersonalSetting()
        {
            InitializeComponent();
            PTB.AddHandler(ButtonEx.MouseLeftButtonDownEvent, new MouseButtonEventHandler(PTB_MouseLeftButtonDown), true);
            PTB.AddHandler(ButtonEx.MouseLeftButtonUpEvent, new MouseButtonEventHandler(PTB_MouseLeftButtonUp), true);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {

            try
            {
                //name = UIData.getInstance().userInfo.name;
                //sex = UIData.getInstance().userInfo.sex;
                //mail = UIData.getInstance().userInfo.mail;
                //birthday = UIData.getInstance().userInfo.birthday;
                //canStrangerChat = UIData.getInstance().userInfo.canStrangerChat;
                //level = UIData.getInstance().userInfo.level;
                //headType = UIData.getInstance().userInfo.headType;
                //headValue = UIData.getInstance().userInfo.headValue;
                //MightList = UIData.getInstance().userInfo.MightList;
                //Current_max_friend_count = UIData.getInstance().userInfo.Current_max_friend_count;
                //Max_friend_count_upper = UIData.getInstance().userInfo.Max_friend_count_upper;
                //Task_score = UIData.getInstance().userInfo.Task_score;
                //appList = UIData.getInstance().userInfo.appList;
                userinfo = UIData.getInstance().userInfo;
                namePTB.Text = userinfo.name;
                if (0 == userinfo.sex) maleRbtn.IsChecked = true;
                else if (1 == userinfo.sex) femaleRbtn.IsChecked = true;
                else if (2 == userinfo.sex) unknowRbtn.IsChecked = true;
                mailPTB.Text = userinfo.mail;
                birthdayDP.Value = Convert.ToDateTime(userinfo.birthday);
                //birthdayDP.DataContext = userinfo.birthday;
                if (0 == userinfo.canStrangerChat) distrubChkBox.IsChecked = false;
                else if (1 == userinfo.canStrangerChat) distrubChkBox.IsChecked = true;
            }
            catch (Exception ee)
            {
                System.Diagnostics.Debug.WriteLine(ee.Message);
            }
            base.OnNavigatedTo(e);
        }
        #region HEAD
        private void camBtnEx_Click(object sender, RoutedEventArgs e)
        {
            CameraCaptureTask camCTask = new CameraCaptureTask();
            camCTask.Show();
            camCTask.Completed += new EventHandler<PhotoResult>(camCTask_Completed);
        }
        void camCTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bmpSource = new BitmapImage();
                bmpSource.SetSource(e.ChosenPhoto);
                //显示头像
                headImg.Source = bmpSource;
                //recRC = new RecordControl()
                //{
                //    MsgType = msgTypeEnum.receivePic,
                //    Pic = bmpSource,
                //};
                //senRC = new RecordControl()
                //{
                //    MsgType = msgTypeEnum.sendPic,
                //    Pic = bmpSource,
                //};
            }
            else
            {
                //do nothing
            }
        }
        #endregion
        #region SEX
        private void maleBtn_Click(object sender, RoutedEventArgs e)
        {
            if (maleRbtn.IsChecked == false)
            {
                maleRbtn.IsChecked = true;
            }
        }

        private void femaleBtn_Click(object sender, RoutedEventArgs e)
        {
            if (femaleRbtn.IsChecked == false)
            {
                femaleRbtn.IsChecked = true;
            }
        }

        private void unknowBtn_Click(object sender, RoutedEventArgs e)
        {
            if (unknowRbtn.IsChecked == false)
            {
                unknowRbtn.IsChecked = true;
            }
        }

        private void maleRbtn_Checked(object sender, RoutedEventArgs e)
        {
            userinfo.sex = 0;
            maleImgSelect.Visibility = System.Windows.Visibility.Visible;
            maleImgUnSelect.Visibility = System.Windows.Visibility.Collapsed;
            maleTxtBlk.Foreground = new SolidColorBrush(Colors.Blue);
        }

        private void maleRbtn_Unchecked(object sender, RoutedEventArgs e)
        {
            maleImgSelect.Visibility = System.Windows.Visibility.Collapsed;
            maleImgUnSelect.Visibility = System.Windows.Visibility.Visible;
            maleTxtBlk.Foreground = new SolidColorBrush(Colors.White);
        }

        private void femaleRbtn_Checked(object sender, RoutedEventArgs e)
        {
            userinfo.sex = 1;
            femaleImgSelect.Visibility = System.Windows.Visibility.Visible;
            femaleImgUnSelect.Visibility = System.Windows.Visibility.Collapsed;
            femaleTxtBlk.Foreground = new SolidColorBrush(Colors.Red);
        }

        private void femaleRbtn_Unchecked(object sender, RoutedEventArgs e)
        {
            femaleImgSelect.Visibility = System.Windows.Visibility.Collapsed;
            femaleImgUnSelect.Visibility = System.Windows.Visibility.Visible;
            femaleTxtBlk.Foreground = new SolidColorBrush(Colors.White);
        }

        private void unknowRbtn_Checked(object sender, RoutedEventArgs e)
        {
            userinfo.sex = 2;
            unknowImgSelect.Visibility = System.Windows.Visibility.Visible;
            unknowImgUnSelect.Visibility = System.Windows.Visibility.Collapsed;
            unknowTxtBlk.Foreground = new SolidColorBrush(Colors.Green);
        }

        private void unknowRbtn_Unchecked(object sender, RoutedEventArgs e)
        {
            unknowImgSelect.Visibility = System.Windows.Visibility.Collapsed;
            unknowImgUnSelect.Visibility = System.Windows.Visibility.Visible;
            unknowTxtBlk.Foreground = new SolidColorBrush(Colors.White);
        }
        #endregion
        #region BIRTHDAY
        private void birthdayDP_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            var dp = sender as DatePicker;
            userinfo.birthday = dp.Value.ToString();
        }
        private void birthdayTxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            birthdayTxtBox.Text = "";
        }
        private void birthdayBtnEx_Click(object sender, RoutedEventArgs e)
        {
            DatePicker datePick1 = new DatePicker();
            //datePick1.ValueChanged += datePicker_ValueChanged;
        }
        #endregion
        #region NAME
        private void namePTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            userinfo.name = namePTB.Text;
        }
        private void nicknameBtnEx_Click(object sender, RoutedEventArgs e)
        {
            birthdayTxtBox.Focus();
        }
        #endregion
        #region EMAIL
        private void mailPTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            userinfo.mail = mailPTB.Text;
        }
        private void emailTxtBox_GotFocus(object sender, RoutedEventArgs e)
        {
            emailTxtBox.Text = "";
        }

        private void emailBtnEx_Click(object sender, RoutedEventArgs e)
        {
            emailTxtBox.Focus();
        }
        #endregion
        #region DISTURB
        private void disturbBtnEx_Click(object sender, RoutedEventArgs e)
        {
            if (distrubChkBox.IsChecked == true)
            {
                distrubChkBox.IsChecked = false;
            }
            else if (distrubChkBox.IsChecked == false)
            {
                distrubChkBox.IsChecked = true;
            }
            else
            {
                //something wrong
            }
        }

        private void distrubChkBox_Checked(object sender, RoutedEventArgs e)
        {
            userinfo.canStrangerChat = 1;
            uncheckedImg.Visibility = System.Windows.Visibility.Collapsed;
            checkedImg.Visibility = System.Windows.Visibility.Visible;
        }

        private void distrubChkBox_Unchecked(object sender, RoutedEventArgs e)
        {
            userinfo.canStrangerChat = 0;
            checkedImg.Visibility = System.Windows.Visibility.Collapsed;
            uncheckedImg.Visibility = System.Windows.Visibility.Visible;
        }
        #endregion
        #region OK
        private void okBtnEx_Click(object sender, RoutedEventArgs e)
        {
            UIData.getInstance().ModifyUser(userinfo);
            if (NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
        }
        #endregion
        #region TEST
        private void PTB_MouseLeftButtonDown(object sender, RoutedEventArgs e)
        {
            PTB.ActionIcon = new BitmapImage(new Uri("/Qplus;component/Pic/user_info_name_down.png", UriKind.RelativeOrAbsolute));
            PTB.Text = "";
        }
        private void PTB_MouseLeftButtonUp(object sender, RoutedEventArgs e)
        {
            PTB.ActionIcon = new BitmapImage(new Uri("/Qplus;component/Pic/user_info_name.png", UriKind.RelativeOrAbsolute));
        }



        /*private void datePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            DateTime date = (DateTime)e.NewDateTime;
            birthdayBtnEx.Content = date.ToString("d");
        }*/
        #endregion
    }
}